﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Quebec.Silverlight.Animations;

namespace Quebec.Silverlight.Extentions
{
	public static class AnimationExtentions
	{
		#region :: AnimateSize ::

		public static void AnimateSize(this FrameworkElement element, double width, double height)
		{
			new SizeAnimation(width, height).Apply(element);
		}
		public static void AnimateSize(this FrameworkElement element, double width, double height, TimeSpan duration)
		{
			new SizeAnimation(width, height, duration).Apply(element);
		}

		#endregion :: AnimateSize ::

		#region :: AnimatePosition ::

		public static void AnimatePosition(this FrameworkElement element, double left, double top)
		{
			new PositionAnimation(left, top).Apply(element);
		}
		public static void AnimatePosition(this FrameworkElement element, double left, double top, TimeSpan duration)
		{
			new PositionAnimation(left, top, duration).Apply(element);
		}

		#endregion :: AnimatePosition ::

		#region :: Fade ::

		public static void FadeOut(this FrameworkElement element)
		{
			element.Fade(0.0);
		}
		public static void FadeOut(this FrameworkElement element, TimeSpan duration)
		{
			element.Fade(0.0, duration);
		}
		public static void FadeOut(this FrameworkElement element, TimeSpan duration, bool switchVisibility)
		{
			element.Fade(0.0, duration, switchVisibility);
		}
		public static void FadeOut(this FrameworkElement element, TimeSpan duration, bool switchVisibility, TimeSpan beginTime)
		{
			element.Fade(0.0, duration, switchVisibility, beginTime);
		}
		public static void FadeOut(this FrameworkElement element, TimeSpan duration, bool switchVisibility, Action onCompletedAction)
		{
			element.Fade(0.0, duration, switchVisibility, onCompletedAction);
		}
		public static void FadeOut(this FrameworkElement element, TimeSpan duration, bool switchVisibility, Action onCompletedAction, TimeSpan beginTime)
		{
			element.Fade(0.0, duration, switchVisibility, onCompletedAction, beginTime);
		}

		public static void FadeIn(this FrameworkElement element)
		{
			element.Fade(1.0);
		}
		public static void FadeIn(this FrameworkElement element, TimeSpan duration)
		{
			element.Fade(1.0, duration);
		}
		public static void FadeIn(this FrameworkElement element, TimeSpan duration, bool switchVisibility)
		{
			element.Fade(1.0, duration, switchVisibility);
		}
		public static void FadeIn(this FrameworkElement element, TimeSpan duration, bool switchVisibility, Action onCompletedAction)
		{
			element.Fade(1.0, duration, switchVisibility, onCompletedAction);
		}

		public static void Fade(this FrameworkElement element, double opacity)
		{
			OpacityAnimation a = new OpacityAnimation(opacity);
			a.AnimationCompleted += (se, ea) =>
			{
				if (opacity == 0)
					element.Visibility = Visibility.Collapsed;
				if (opacity == 1)
					element.Visibility = Visibility.Visible;
			};
			a.Apply(element);
		}

		public static void Fade(this FrameworkElement element, double opacity, TimeSpan duration)
		{
			OpacityAnimation a = new OpacityAnimation(opacity, duration);
			a.AnimationCompleted += (se, ea) =>
			{
				if (opacity == 0)
					element.Visibility = Visibility.Collapsed;
				if (opacity == 1)
					element.Visibility = Visibility.Visible;
			};
			a.Apply(element);
		}

		public static void Fade(this FrameworkElement element, double opacity, TimeSpan duration, bool switchVisibility)
		{
			OpacityAnimation a = new OpacityAnimation(opacity, duration);
			a.AnimationCompleted += (se, ea) =>
			{
				if (switchVisibility)
				{
					if (opacity == 0)
						element.Visibility = Visibility.Collapsed;
					if (opacity == 1)
						element.Visibility = Visibility.Visible;
				}
			};
			a.Apply(element);
		}

		public static void Fade(this FrameworkElement element, double opacity, TimeSpan duration, bool switchVisibility, TimeSpan beginTime)
		{
			OpacityAnimation a = new OpacityAnimation(opacity, duration, beginTime);
			a.AnimationCompleted += (se, ea) =>
			{
				if (switchVisibility)
				{
					if (opacity == 0)
						element.Visibility = Visibility.Collapsed;
					if (opacity == 1)
						element.Visibility = Visibility.Visible;
				}
			};
			a.Apply(element);
		}

		public static void Fade(this FrameworkElement element, double opacity, TimeSpan duration, bool switchVisibility, Action onCompletedAction)
		{
			OpacityAnimation a = new OpacityAnimation(opacity, duration);
			a.AnimationCompleted += (se, ea) =>
			{
				if (switchVisibility)
				{
					if (opacity == 0)
						element.Visibility = Visibility.Collapsed;
					if (opacity == 1)
						element.Visibility = Visibility.Visible;
				}

				onCompletedAction();
			};
			a.Apply(element);
		}

		public static void Fade(this FrameworkElement element, double opacity, TimeSpan duration, bool switchVisibility, Action onCompletedAction, TimeSpan beginTime)
		{
			OpacityAnimation a = new OpacityAnimation(opacity, duration, beginTime);
			a.AnimationCompleted += (se, ea) =>
			{
				if (switchVisibility)
				{
					if (opacity == 0)
						element.Visibility = Visibility.Collapsed;
					if (opacity == 1)
						element.Visibility = Visibility.Visible;
				}

				onCompletedAction();
			};
			a.Apply(element);
		}

		public static void CrossFade(this FrameworkElement elementToFadeOut, FrameworkElement elementToFadeIn)
		{
			FadeOut(elementToFadeOut);
			FadeIn(elementToFadeIn);
		}
		public static void CrossFade(this FrameworkElement elementToFadeOut, FrameworkElement elementToFadeIn, TimeSpan duration)
		{
			FadeOut(elementToFadeOut, duration);
			FadeIn(elementToFadeIn, duration);
		}

		#endregion :: Fade ::
	}
}
